package zsc.ruanc.practicaltraining.security.handler;

import lombok.SneakyThrows;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;
import zsc.ruanc.practicaltraining.model.result.JsonResult;
import zsc.ruanc.practicaltraining.utils.ResultUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 权限不足时对异常的处理
 */
@Component
public class SAccessDeniedHandler extends JSONResult implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {




        JsonResult error = ResultUtils.error(-13, "权限不足：" + e.getMessage());

        this.WriteJson(httpServletRequest, httpServletResponse, error);

    }
}
